#include "messageeditarea.h"
#include "historymessagewidget.h"
#include "debug.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QStyleOption>
#include <QPainter>
#include <QScrollBar>

MessageEditArea::MessageEditArea(QWidget *parent)
    : QWidget(parent)
{
    QString hBtnStyle = "QPushButton { background-color : rgb(245, 245, 245); border : none; }";
    hBtnStyle += "QPushButton:hover { background-color : rgb(240, 240, 240); }";
    hBtnStyle += "QPushButton:pressed { background-color : rgb(235, 235, 235); }";

    QString textEditStyle = "QPlainTextEdit { border : none; background-color : transparent; font-size : 15px; padding : 10px; }";

    QString vScrollBarStyle = "QScrollBar:vertical { width : 5px; background : none; border : none; margin : 10 px; }";
    vScrollBarStyle += "QScrollBar::handle:vertical { background: #c0c0c0; min-height: 20px; border-radius: 5px; }";
    vScrollBarStyle += "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; }";
    vScrollBarStyle += "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: none; }";

    QString hScrollBarStyle = "QScrollBar:horizontal { height : 0px; }";

    QString sendTextBtnStyle = "QPushButton { background-color: rgb(7, 193, 96); border : none; border-radius : 10px; }";
    sendTextBtnStyle += "QPushButton:hover { background-color: rgb(6, 183, 91); }";
    sendTextBtnStyle += "QPushButton:pressed { background-color: rgb(6, 174, 86); }";

    sendImageBtn = new QPushButton();
    sendImageBtn->setStyleSheet(hBtnStyle);
    sendImageBtn->setFixedSize(QSize(35, 35));
    sendImageBtn->setIconSize(QSize(25, 25));
    sendImageBtn->setIcon(QIcon(":/resource/image/image.png"));

    sendFileBtn = new QPushButton();
    sendFileBtn->setStyleSheet(hBtnStyle);
    sendFileBtn->setFixedSize(QSize(35, 35));
    sendFileBtn->setIconSize(QSize(25, 25));
    sendFileBtn->setIcon(QIcon(":/resource/image/file.png"));

    sendSpeechBtn = new QPushButton();
    sendSpeechBtn->setStyleSheet(hBtnStyle);
    sendSpeechBtn->setFixedSize(QSize(35, 35));
    sendSpeechBtn->setIconSize(QSize(25, 25));
    sendSpeechBtn->setIcon(QIcon(":/resource/image/sound.png"));

    getHistoryBtn = new QPushButton();
    getHistoryBtn->setStyleSheet(hBtnStyle);
    getHistoryBtn->setFixedSize(QSize(35, 35));
    getHistoryBtn->setIconSize(QSize(25, 25));
    getHistoryBtn->setIcon(QIcon(":/resource/image/history.png"));

    sendTextBtn = new QPushButton();
    sendTextBtn->setText("发送");
    sendTextBtn->setFixedSize(QSize(96, 32));
    sendTextBtn->setStyleSheet(sendTextBtnStyle);

    textEdit = new QPlainTextEdit();
    textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    textEdit->setStyleSheet(textEditStyle);
    textEdit->verticalScrollBar()->setStyleSheet(vScrollBarStyle);
    textEdit->horizontalScrollBar()->setStyleSheet(hScrollBarStyle);

    // 水平布局管理器, 用于组织按钮水平排列
    QHBoxLayout * hLayout = new QHBoxLayout();
    hLayout->setSpacing(10);
    hLayout->setContentsMargins(0, 0, 0, 0);
    hLayout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    hLayout->addWidget(sendImageBtn);
    hLayout->addWidget(sendFileBtn);
    hLayout->addWidget(sendSpeechBtn);
    hLayout->addWidget(getHistoryBtn);

    // 垂直布局管理器, 用于组织消息编辑区布局排列
    QVBoxLayout * vLayout = new QVBoxLayout();
    vLayout->setSpacing(0);
    vLayout->setContentsMargins(10, 10, 20, 20);
    vLayout->setAlignment(Qt::AlignTop);
    vLayout->addLayout(hLayout);
    vLayout->addWidget(textEdit);
    vLayout->addWidget(sendTextBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    // 设置区域必要属性
    this->setFixedHeight(200);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    this->setObjectName("messageEidtArea");
    this->setStyleSheet("#messageEidtArea { border-top : 1px solid rgb(230, 230, 230); }");
    this->setLayout(vLayout);

    // 8. 统一初始化信号槽
    initSignalSlot();
}

///
/// \brief SessionFriendItem::paintEvent 重写绘画事件，让自定义控件支持样式设置
/// \param event 事件
///
void MessageEditArea::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void MessageEditArea::initSignalSlot()
{
    // 1. 关联 "显示历史消息窗口" 信号槽
    connect(getHistoryBtn, &QPushButton::clicked, this, [=]() {
#if TEST_UI
        QMessageBox::information(this, "UI测试", "模拟向服务器发送获取会话标识请求");
#else
        if (dataCenter->getCurrentChatSessionId().isEmpty()) {
            return;
        }
#endif
        HistoryMessageWidget* historyMessageWidget = new HistoryMessageWidget(this);
        historyMessageWidget->exec();
    });
}
